package cn.bugstack.domain.auth.service.impl;


import cn.bugstack.domain.auth.adapter.port.ILoginPort;
import cn.bugstack.domain.auth.service.ILoginService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @author 丨一
 * @version 1.0
 * @title LoginServiceImpl
 * @description:
 * @date 2024/11/10 19:20
 */

@Service
public class LoginServiceImpl implements ILoginService {


    @Resource
    private ILoginPort loginPort;

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 获取 AccessToken -> 先检查缓存,没有的话向微信接口平台发起调用
     *
     * @return
     * @throws Exception
     */
    @Override
    public String createQrCodeTicket() throws Exception {
        return loginPort.createQrCodeTicket();
    }

    @Override
    public String checkLogin(String ticket) {
        return loginPort.checkLogin(ticket);
    }

    @Override
    public void saveLoginState(String ticket, String openid) throws IOException {
        redisTemplate.opsForValue().setIfAbsent(ticket, openid, 1, TimeUnit.HOURS);
        loginPort.sendLoginTemplate(openid);
    }
}
